#include "Day.h"
#include <sstream>


const int INIT_ARR_SIZE = 7;
const int EXPAND_VALUE = 7;

void Day::clearList(){
	for (unsigned int i = 0; i < this->mList.size(); i++){
		delete mList[i];
	}
	this->mList.clear();
}

Day::Day(void){
}

Day::~Day(void){
	this->clearList();
}
Day::Day(const Day& day){
	const vector<Meeting *> & lst = day.mList;
	for (unsigned int i=0; i < lst.size(); i++) {
		Meeting *m = new Meeting(*(lst[i]));
		if (m == NULL){
			cerr << "ERROR: couldn't allocate mem for Day()" << endl;
		}
		this->mList.push_back(m);
	}
}
const Day& Day::operator =(const Day &day){

	if (this != &day){

		this->clearList();
		for (unsigned int i=0; i<day.mList.size(); i++) {
			Meeting *m = new Meeting(*(day.mList[i]));
			if (m == NULL){
				cerr << "ERROR: couldn't allocate memory for Meeting()" << endl;
			}
			this->mList.push_back(m);
		}
	}

	return *this;
}

const vector<Meeting *> & Day::getMeetingList() const{
	return this->mList;
}

// add a meeting to the day iff there isn't a conflicting meeting
bool Day::addMeeting(Meeting &meeting){
	for (unsigned int i=0; i < mList.size(); i++) {
		if (meeting.checkColission(*mList[i]))
			return false;
	}
	
	this->mList.push_back(&meeting);
	return true;
}

// remove a meeting from the day
bool Day::removeMeeting(Meeting & meeting){	
	for (unsigned int i = 0; i < mList.size(); i++){
		if (this->mList[i]->compare(meeting)){
			//remove the i-th element
			delete this->mList[i];
			this->mList.erase(mList.begin()+i);
			return true;
		}
	}
	return false;
}

// return meeting by the given time
// if the meeting doesn't exists
Meeting* Day::getMeeting(float startTime) const{
	for (unsigned int i = 0; i < this->mList.size(); i++){
		if (this->mList[i]->getTimeStart() == startTime){
			return this->mList[i];
		}
	}

	return NULL;
}

int Day::numOfMeetings() const {
	return mList.size();
}

void Day::cleanDay(){
	this->clearList();
}

ostream & Day::toString(ostream & os) const{
	const vector<Meeting *> & lst = this->mList;

	if (lst.size() == 0){
		os << "NO MEETINGS" << endl;
	}
	else {
		for (unsigned int i = 0; i < lst.size(); i++){
			//os << lst[i]->getTimeStart() << ", " << lst[i]->getTimeEnd() << ", " << lst[i]->getSubject() << endl;
			lst[i]->toString(os);
		}
	}

	return os;
}
//
//int main(int argc, char **argv){
//	Day d;
//	d.toString(cout);
//	
//	cout << endl;
//
//	//d.addMeeting(*(new Meeting(1, 2, "to do stuff")));
//
//	d.toString(cout);
//
//	cout << "DONE" << endl;
//
//	// BUG
//	d.addMeeting(*(new Meeting(-1, -2, "sdf")));
//	d.toString(cout);
//	//END OF BUG
//
//	char buffer[1024];
//	for (int i = 0; i < 100; i++){
//		d.addMeeting(*(new Meeting(i, i+1, "meeting #"+std::string(_itoa(i, buffer, 10)) )));
//	}
//
//	cout << "MEETINGS:" << endl;
//	d.toString(cout);
//	cout << endl;
//	cout << "number of meetings: " << d.numOfMeetings() << endl;
//
//	for (int i = 0; i < 50; i++){
//		Meeting * mPtr = d.getMeeting(i);
//		if (mPtr != NULL){
//			d.removeMeeting(*mPtr);
//		}
//	}
//
//	cout << "MEETINGS:" << endl;
//	d.toString(cout);
//	cout << endl;
//
//	cout << "number of meetings: " << d.numOfMeetings() << endl;
//
//	cout.clear();
//
//	getchar();
//	return 0;
//}